package com.dream.wukong.configcenter.client;

import com.dream.wukong.configcenter.client.core.ICallback;
import com.dream.wukong.configcenter.client.core.InitConfig;
import com.dream.wukong.configcenter.client.core.ProxyProperty;
import com.dream.wukong.configcenter.client.core.storage.DynamicConfig;
import com.dream.wukong.configcenter.client.core.util.ConfigConstant;
import com.dream.wukong.configcenter.client.remote.http.HttpInitialize;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import org.apache.log4j.Logger;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 11:26 2018/9/19.
 * @Modified:
 */
public class ConfigCenter {

        private static Logger logger = Logger.getLogger(ConfigCenter.class);

        private InitConfig initConfig =  InitConfig.getInstance();

        public ConfigCenter() {
        }

        /**
         * 描  述:  设置要获取的serviceName 以及对应的IP地址和端口号  如要取log（工程）的配置则设置serviceName 为log
         * 参  数:serviceName
         * 参  数:configIp
         * 参  数:configPort
         * 返回值: com.hollycrm.hollybeacons.configcenter.client.ConfigCenter
         * 创建时间: 2018/5/23
         */
        public ConfigCenter setServiceName(String serviceName, String configIp,
                                           String configPort){
            initConfig.setServiceName(serviceName);
            initConfig.setConfigIp(configIp);
            initConfig.setConfigPort(configPort);
            return this;
        }

        /**
         * 描  述:  设置要获取的serviceName 如要取log（工程）的配置则设置serviceName 为log
         * 参  数:serviceName
         * 参  数:configIp
         * 参  数:configPort
         * 返回值: com.hollycrm.hollybeacons.configcenter.client.ConfigCenter
         * 创建时间: 2018/5/23
         */
        public ConfigCenter setServiceName(String serviceName){
            initConfig.setServiceName(serviceName);
            initConfig.setConfigIp(ConfigConstant.DEFAULT_CONFIG_IP);
            initConfig.setConfigPort(ConfigConstant.DEFAULT_CONFIG_PORT);
            return this;
        }

        /**
         * 描  述:  设置定时获取数据的频率及是否忽略被清除的配置（默认为false）
         * 参  数:initialDelayMillis
         * 参  数:delayMillis
         * 参  数:ignoreDeletesFromSource
         * 返回值: com.hollycrm.hollybeacons.configcenter.client.ConfigCenter
         * 创建时间: 2018/5/23
         */
        public ConfigCenter setScheduler(int initialDelayMillis, int delayMillis,
                                         boolean ignoreDeletesFromSource){
            initConfig.setInitialDelayMillis(initialDelayMillis);
            initConfig.setDelayMillis(delayMillis);
            initConfig.setIgnoreDeletesFromSource(ignoreDeletesFromSource);
            return  this;
        }

        public ConfigCenter initConfig(){
            HttpInitialize.getInstance();
            return this;
        }


        public ProxyProperty getDataSource(String dataSource){
            HttpInitialize.getInstance();
            return DynamicConfig.getDataSource(dataSource);
        }




        public static ConfigCenter getInstance() {
            return ConfigCenterHolder.INSTANCE;
        }

        protected Object readResolve() {
            return ConfigCenterHolder.INSTANCE;
        }

        /**
         * 描  述: 为资源设置变更监听,当配置信息发送改变时触发
         * 参  数:sourceName
         * 参  数:callback
         * 返回值: com.hollycrm.hollybeacons.configcenter.client.ConfigCenter
         * 创建时间: 2018/5/23
         */
        public ConfigCenter addSourceListener(String sourceName , ICallback callback){
            logger.info(sourceName + ": 新增监听开始");
            DynamicStringProperty dynamicStringProperty = DynamicPropertyFactory.getInstance().getStringProperty(sourceName, "");
            dynamicStringProperty.addCallback(() -> {
                        try {
                            callback.callback();
                        } catch (Exception e) {
                            logger.error(sourceName+": callback 失败" , e);
                        }
                    }
            );
            logger.info(sourceName + ": 新增监听结束");
            return this;
        }

        private static final class ConfigCenterHolder {
            private static final ConfigCenter INSTANCE = new ConfigCenter();
        }

//    public static void main(String[] args) {
//        ConfigCenter configCenter = ConfigCenter.getInstance().setServiceName("hah","172.16.52.66","10100").initConfig();
//
//        ConfigCenter.getInstance().addSourceListener("jdbc",() ->{
//
//            ProxyProperty jdbc = ConfigCenter.getInstance().getDataSource("jdbc");
//
//            System.out.println("url" + jdbc.getProperty("url"));
//
//        });
//
//        System.out.println("ip : " + configCenter.getDataSource("jdbc").getProperty("url"));
//
//
//        while (true){
//            try {
//                System.out.println(System.getProperty("name"));
//                Thread.sleep(6100L);
//
//                System.out.println("ip : " + configCenter.getDataSource("jdbc").getProperty("url"));
//
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }

    }
